package crotrol;

import entity.News;
import entity.NewsCategory;
import mysql.NewsDao;
import mysql.BaseDao;

import java.sql.*;


public class NewsControl extends BaseDao implements NewsDao {


    //根据标题查询
    public ResultSet getNewsById(News news){
        try {
            String sql = "select id,title,summary,createDate from news_detail where id=?";
            Object[] params = {news.getId()};
            this.executeQuery(sql,params);
            while (resultSet.next()){
                String summary = resultSet.getString("summary");
                System.out.println(summary);
            }
            System.out.println(resultSet.getRow());
            return resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            destroy();
        }
    }
    //增加
    public void addNews(News news){
        String sql = "INSERT INTO news_detail(`id`,`categoryId`,`title`,`summary`,`content`,`author`,`createDate`) VALUES(?,?,?,?,?,?,?)";
        Object[] params = {news.getId(),news.getCategoryId(),news.getTitle(),news.getSummary(),news.getContent(),news.getAuthor(),news.getCreateDate()};
        int i = this.executeUpdate(sql,params);
            if (i>0){
                System.out.println("成功插入"+i+"条数据");
            }
        }
    //删除
    public void deleteNews(News news){
        String sql = "delete from news_detail where id=?";
        Object[] params = {news.getId()};
        int i = this.executeUpdate(sql,params);
        if (i>0){
            System.out.println("删除成功");
        }
    }
    //修改
    public void UpdateNews(News news){
        String sql = "update news_detail set title=? where id=?";
        Object[] params = {news.getTitle(),news.getId()};
        int i = this.executeUpdate(sql,params);
        if (i>0){
            System.out.println("修改成功");
        }
    }

    @Override
    public int getNewsCount(NewsCategory newsCategory) {
        String sql = "select id from news_detail where categoryid=?";
        Object[] params = {newsCategory.getId()};
        ResultSet resultSet = this.executeQuery(sql,params);
        try {
            resultSet.last();
            return resultSet.getRow();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public void getAllNews(){
        String sql = "select * from news_detail";
        Object[] params = {};
        ResultSet resultSet = this.executeQuery(sql,params);
        try {
            while (resultSet.next()){
                String summary = resultSet.getString("summary");
                System.out.println(summary);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        NewsControl mysqlControl = new NewsControl();
        News news = new News();
        news.setId(3);
        mysqlControl.getNewsById(news);
        mysqlControl.getAllNews();
    }
}
